/*
*  Matrix2.cpp
*
*  Created on: Jan 14, 2012
*  
*/

#include  "Matrix2.h"

namespace Physics
{
	Matrix2::Matrix2()
	{
		xx = xy = yx = yy = 0.0f;
	}

	Matrix2::Matrix2( float XX, float XY,
					  float YX, float YY)
	{
		xx = XX;
		xy = XY;
		yx = YX;
		yy = YY;
	}

	Matrix2::~Matrix2()
	{

	}

	// figure out a new approach to this....might have to code a [] operator
	float Matrix2::getIndex(int row, int col)
	{
		float value = 0.0f;

		switch(row)
		{
		case 0:

			switch(col)
			{
				case 0:
					value = xx;
					break;

				case 1:
					value = xy;
					break;

				default:
					value = 0.0f;
					break;
			}
			
			break;

		case 1:

			switch(col)
			{
				case 0:
					value = yx;
					break;

				case 1:
					value = yy;
					break;

				default:
					value = 0.0f;
					break;

			}
			
			break;

		default:
			value = 0.0f;
			break;

		}

		return value;
	}


	void Matrix2::identity()
	{
		xx = yy = 1.0f;
		xy = yx = 0.0f;
	}

	Matrix2 Matrix2::operator +(Matrix2 &matrix)
	{
		return Matrix2(xx + matrix.getIndex(0,0), xy + matrix.getIndex(0,1),
					   yx + matrix.getIndex(1,0), yy + matrix.getIndex(1,1) );

	}

	Matrix2 Matrix2::operator -(Matrix2 &matrix)
	{
		return Matrix2(xx - matrix.getIndex(0,0), xy - matrix.getIndex(0,1),
					   yx - matrix.getIndex(1,0), yy - matrix.getIndex(1,1));
	}
	
	// Had an issue when programming it. Each calculation is correct,
	// but when making the matrix to return it it had issues
	Matrix2 Matrix2::operator *(Matrix2 &matrix)
	{	
		Matrix2 result( 
						(xx * matrix.getIndex(0,0)) + (xy * matrix.getIndex(1,0)),
						(xx * matrix.getIndex(0,1)) + (xy * matrix.getIndex(1,1)),
	
						(yx * matrix.getIndex(0,0)) + (yy * matrix.getIndex(1,0)),
						(yx * matrix.getIndex(0,1)) + (yy * matrix.getIndex(1,1)) );

		return result;
	}
		 
	void Matrix2::Scale(float scaleX, float scaleY)
	{
		xx *= scaleX; xy *= scaleX;
		yx *= scaleY; yy *= scaleY;			
	}

	void Matrix2::Scale(float scaleAmount)
	{
		Scale(scaleAmount, scaleAmount);
	}
} /* NAMESPACE */